package com.xhwl.data.mq.producer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;

@Component
public class EntPermissionProducer {

    private static String ENTERPRISE_PERMISSION_CREATE = "enterprise-permission-create";
    private static String ENTERPRISE_PERMISSION_UPDATE = "enterprise-permission-update";

    @Autowired
    private KafkaTemplate<String, Object> kafkaTemplate;

    private final Logger log = LoggerFactory.getLogger(AccountProducer.class);

    public void createEntPermission(String content) {
        //发送消息 send 方法可以指定分区或者不指定分区
        ListenableFuture<SendResult<String, Object>> future = kafkaTemplate.send(ENTERPRISE_PERMISSION_CREATE, content);
        future.addCallback(new ListenableFutureCallback<SendResult<String, Object>>() {
            @Override
            //推送成功
            public void onSuccess(SendResult<String, Object> result) {
                log.info("主题={}， 企业创建数据推送成功={}", ENTERPRISE_PERMISSION_CREATE, content);
            }
            @Override
            //推送失败
            public void onFailure(Throwable ex) {
                log.info("主题={}， 企业创建数据推送失败={}", ENTERPRISE_PERMISSION_CREATE, content);
            }
        });
    }

    public void updateEntPermission(String content) {
        ListenableFuture<SendResult<String, Object>> future = kafkaTemplate.send(ENTERPRISE_PERMISSION_UPDATE, content);
        future.addCallback(new ListenableFutureCallback<SendResult<String, Object>>() {
            @Override
            //推送成功
            public void onSuccess(SendResult<String, Object> result) {
                log.info("主题={}， 企业更新数据推送成功={}", ENTERPRISE_PERMISSION_UPDATE, content);
            }
            @Override
            //推送失败
            public void onFailure(Throwable ex) {
                log.info("主题={}， 企业更新数据推送失败={}", ENTERPRISE_PERMISSION_UPDATE, content);
            }
        });
    }

}
